<?php
declare (strict_types = 1);

namespace app\adminapi\middleware;

use app\common\logic\system\AdminLogic;
use app\common\service\JsonService;
use think\Response;

/**
 * 登录中间件
 */
class Login
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        $token = $request->header('Authorization');

        if(empty($token) && !$request->controllerObject->isNotNeedLogin()){
            JsonService::notLoginError();
        }

        if($token){
            $adminInfo = (new AdminLogic())->checkToken($token);
            $request->adminInfo = $adminInfo;
            $request->adminId = $adminInfo['admin_id'];
        }


        return $next($request);
    }
}
